package com.alks.function.controller.procurement;


import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.Idempotent;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.procurement.CommonMaterialDetailDto;
import com.alks.function.data.dto.procurement.NotOrderDetailsDto;
import com.alks.function.data.dto.procurement.PurchaseOrderDto;
import com.alks.function.data.request.chengKong.CommonQueryRequest;
import com.alks.function.data.request.procurement.*;
import com.alks.function.service.chengKong.GeneralMaterialsService;
import com.alks.function.service.procurement.MaterialClearanceService;
import com.alks.function.service.procurement.SporadicClearanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@ApiRestController("/SporadicClearanceOperation")
@Slf4j
@Api(tags = {"采购部-零星请购作业-材料","采购部-零星请购作业-订购处理"})
@RoleCheck(prgIds = {"150103"})
public class SporadicClearanceOperationController {

    @Autowired
    private SporadicClearanceService SporadicClearanceService;

    @Autowired
    private MaterialClearanceService materialClearanceService;

    @Autowired
    private GeneralMaterialsService generalMaterialsService;

    @PostMapping("/getCommonMaterialDetail")
    @ApiOperation(value = "选择通用材料：通用材料明细查询",tags = "采购部-零星请购作业-材料")
    public ResponseInfo getCommonMaterialDetail(@RequestBody CommonQueryRequest request) {
//        return SporadicClearanceService.getCommonMaterialDetail(request);
        return generalMaterialsService.getPublicMaterials2(request);
    }

    @PostMapping("/getBatchProcess")
    @ApiOperation(value = "选择通用材料：待请购批处理",tags = "采购部-零星请购作业-材料")
    public ResponseInfo getBatchProcess(@RequestBody List<CommonMaterialDetailDto> request) {
        return SporadicClearanceService.getBatchProcess(request);
    }

    @ApiOperation(value = "待定购明细:查询",tags = "采购部-零星请购作业-订购处理")
    @GetMapping("/getNotOrderDetailsList")
    public ResponseInfo getNotOrderDetailsList() {
        return SporadicClearanceService.getNotOrderDetailsList();
    }

    @ApiOperation(value = "待定购明细:修改",tags = "采购部-零星请购作业-订购处理")
    @PostMapping("/updateNotOrderDetails")
    public ResponseInfo updateNotOrderDetails(@RequestBody List<NotOrderDetailsDto> request) {
        return SporadicClearanceService.updateNotOrderDetails(request);
    }

    @ApiOperation(value = "待定购明细:删除",tags = "采购部-零星请购作业-订购处理")
    @PostMapping("/deleteNotOrderDetails")
    public ResponseInfo deleteNotOrderDetails(@RequestBody NotOrderDetailsRequest request) {
        return SporadicClearanceService.deleteNotOrderDetails(request);
    }

    @ApiOperation(value = "生成订购资料",tags = "采购部-零星请购作业-订购处理")
    @PostMapping("/makeOrderDetails")
    @Idempotent(
            key = "'TY_getBatchProcess_' + #request.![lotNo + '_' + mtlColor + '_' + mtlName + '_' + mtlSpec + '_' + mtlUnit + '_' + sysUser].stream().sorted().collect(T(java.util.stream.Collectors).joining(','))",
            expireTime = 5,
            info = "请勿重复提交，请检查是否已重复处理过相同材料，如已处理，请刷新后再试"
    )
    public ResponseInfo makeOrderDetails(@RequestBody List<NotOrderDetailsRequest> request) {
        return SporadicClearanceService.makeOrderDetails(request);
    }

    @WebLogger
    @PostMapping("/makePurchaseOrder")
    @ApiOperation(value = "进入我的订购单：形成订购单",tags = "采购部-零星请购作业-订购处理")
    public ResponseInfo makePurchaseOrder(@RequestBody LotNoRequest4 r){
        return SporadicClearanceService.makePurchaseOrder(r);
    }

    @ApiOperation(value = "进入我的订购单：查询",tags = "采购部-零星请购作业-订购处理")
    @PostMapping("/getMyPurchaseOrder")
    public ResponseInfo getMyPurchaseOrder(@RequestBody PurchaseOrderRequest request) {
        return SporadicClearanceService.getMyPurchaseOrder(request);
    }

//    @ApiOperation(value = "进入所有订购单：指令明细查询",tags = "采购部-零星请购作业-订购处理")
//    @PostMapping("/getAllPurchaseOrderLotDetail")
//    public ResponseInfo getAllPurchaseOrderLotDetail(@RequestBody PurchaseOrderDto request) {
//        return SporadicClearanceService.getAllPurchaseOrderLotDetail(request);
//    }
    @WebLogger
    @PostMapping("/updatePurchaseOrder")
    @ApiOperation(value = "进入我的订购单：编辑",tags = "采购部-零星请购作业-订购处理")
    public ResponseInfo updatePurchaseOrder(@RequestBody List<PurchaseOrderDto> list){
        return SporadicClearanceService.updatePurchaseOrder(list);
    }

    @WebLogger
    @PostMapping("/deletePurchaseOrder")
    @ApiOperation(value = "进入我的订购单：勾选删除",tags = "采购部-零星请购作业-订购处理")
    public ResponseInfo deletePurchaseOrder(@RequestBody List<LotNoRequest6> list){
        return SporadicClearanceService.deletePurchaseOrder(list);
    }

//    @ApiOperation(value = "进入我的订购单：插入号码",tags = "采购部-零星请购作业-订购处理")
//    @PostMapping("/insertNumber")
//    public ResponseInfo insertNumber(@RequestBody PurchaseOrderRequest request) {
        //todo 史倩待完成
//        return SporadicClearanceService.insertNumber(request);
//    }

    @ApiOperation(value = "进入所有订购单:查询",tags = "采购部-零星请购作业-订购处理")
    @PostMapping("/getAllPurchaseOrder")
    public ResponseInfo getAllPurchaseOrder(@RequestBody PurchaseOrderRequest request) {
        return SporadicClearanceService.getAllPurchaseOrder(request);
    }

    @ApiOperation(value = "进入我的订购单/进入所有订购单：指令明细查询",tags = "采购部-零星请购作业-订购处理")
    @PostMapping("/getAllPurchaseOrderLotDetail")
    public ResponseInfo getPurchaseOrderLotDetail(@RequestBody PurchaseOrderDto request) {
        return SporadicClearanceService.getPurchaseOrderLotDetail(request);
    }

    @WebLogger
    @GetMapping("/exportAllPurchaseOrder")
    @ApiOperation(value = "进入所有订购单:导出订购单",tags = "采购部-零星请购作业-订购处理")
    public void exportAllPurchaseOrder(@RequestParam String purNo, HttpServletResponse response){
        SporadicClearanceService.exportAllPurchaseOrder(purNo,response);
    }

    @WebLogger
    @GetMapping("/exportMyPurchaseOrder")
    @ApiOperation(value = "进入我的订购单:导出订购单",tags = "采购部-零星请购作业-订购处理")
    public void exportMyPurchaseOrder(@RequestParam String purNo, HttpServletResponse response){
        SporadicClearanceService.exportAllPurchaseOrder(purNo,response);
    }

}
